package tingshu.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import tingshu.repository.AlbumRepo;
import tingshu.util.Log;
import tingshu.view_object.CandidateListView;
import tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 展示该专辑中未被绑定的音频
 * @author houdaiqin
 * @date 2022/07/20 12:03
 **/
@WebServlet("/studio/album/candidate.json")
public class CandidateJsonServlet extends HttpServlet {
    private final AlbumRepo albumRepo=new AlbumRepo();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserVO currentUser=null;
        HttpSession session=req.getSession(false);
        if (session!=null){
            currentUser=(UserVO) session.getAttribute("currentUser");
        }else{
            Log.println("用户未登录");
            return;
        }
        if (currentUser==null){
            Log.println("用户未登录");
            return;
        }
        int aid = Integer.parseInt(req.getParameter("aid"));
        CandidateListView candidateListView=new CandidateListView();
        candidateListView.aid=aid;
        candidateListView.currentUser=currentUser;
        candidateListView.trackViewList= albumRepo.selectTidAndTitleByUidAndAid(currentUser.uid,aid);


        ObjectMapper objectMapper=new ObjectMapper();
        String json=objectMapper.writeValueAsString(candidateListView);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        PrintWriter writer=resp.getWriter();
        writer.println(json);
    }
}